#include "PlayState.h"

PlayState::PlayState(GameContext& gameC)
	: GameState(gameC)
	, mActionChar(false)
{}
PlayState::~PlayState()
{}
void PlayState::Load()
{
	mPlayBG.Load("play_bg.png");
	mMap.Load("Level01.txt", "Texturepack01.txt");
	LoadInitialPositionCharacter();
	LoadInitialPositionEnemy();
	mCursor.Load("sword.png");
	char levelName[256];
	char textureName[256];
	sprintf_s(levelName, 256, "level0%d.txt", mGameContext.GetLevel());
	sprintf_s(textureName, 256, "Texturepack0%d.txt", mGameContext.GetLevel());

	mFont.Load(12);
}
void PlayState::Unload()
{
	mPlayBG.Unload();
	mChar[0].Unload();
	mChar[1].Unload();
	mChar[2].Unload();
	mChar[3].Unload();

	mEnemy[0].Unload();
	mEnemy[1].Unload();

	mMap.Unload();
	mCursor.Unload();
	mFont.Unload();
}
NextStates PlayState::Update(float deltaTime)
{
	mCursor.Update(deltaTime);
	CheckCharAction(deltaTime);
	mPlayBG.Update(deltaTime);
	for(int i(0); i < 2; ++i)
	{
		mEnemy[i].Update(deltaTime);
	}
	NextStates nextState = None;
	if(Input_IsKeyPressed(Keys::ESCAPE))
	{
		nextState = Frontend;
	}
	if(CheckGameOver() == true)
	{
		nextState = GameOver;
	}
	return nextState;
}
void PlayState::Render()
{
	mPlayBG.Render();
	mMap.Render();
	RenderChar();
	RenderEnemy();
	mCursor.Render();
	DrawDebug();
	DrawText();
}
void PlayState::CheckCharAction(float deltaTime)
{
	if(Input_IsMousePressed(Mouse::LBUTTON) && mActionChar == false)
	{
		SVector2 mousePos(Input_GetMouseScreenX(), Input_GetMouseScreenY());
		mousePos -= mMap.GetOffset();
		for(int i = 0; i < 5; ++i)
		{
			if(mousePos.x <= (mChar[i].GetCharacterPos().x + 32)
				&&	mousePos.x >= (mChar[i].GetCharacterPos().x))
			{
				if(mousePos.y <= (mChar[i].GetCharacterPos().y + 32)
					&&	mousePos.y >= (mChar[i].GetCharacterPos().y))
				{
					mChar[i].IsClicked();
					mActionChar = true;
				}
			}
		}
	}

	if(Input_IsMousePressed(Mouse::LBUTTON) && mActionChar == false)
	{
		SVector2 mousePos(Input_GetMouseScreenX(), Input_GetMouseScreenY());
		mousePos -= mMap.GetOffset();
		for(int i = 0; i < 2; ++i)
		{
			if(mousePos.x <= (mEnemy[i].GetEnemyPos().x + 50)
				&&	mousePos.x >= (mEnemy[i].GetEnemyPos().x))
			{
				if(mousePos.y <= (mEnemy[i].GetEnemyPos().y + 50)
					&&	mousePos.y >= (mEnemy[i].GetEnemyPos().y))
				{
					mEnemy[i].IsClicked();
					mActionChar = false;
				}
			}
		}
	}
	if(Input_IsKeyPressed(Keys::M))
	{
		if(mActionChar)
		{
			MoveCharacter();
		}
		else
		{
			MoveEnemy();
		}
		mActionChar = false;
	}
	else if(Input_IsKeyPressed(Keys::A))
	{
		if(mActionChar)
		{
			AttackCharacter();
		}
		else
		{
			AttackEnemy();
		}
		mActionChar = false;
	}
	for(int i = 0; i < 4; ++i)
	{
 		mChar[i].Update(deltaTime);
	}
}
void PlayState::LoadInitialPositionCharacter()
{
	mStorePos[0] = mMap.GetScreenPositionPixelChar(SVector2(0.0f, 0.0f));
	mStorePos[1] = mMap.GetScreenPositionPixelChar(SVector2(0.0f, 1.0f));
	mStorePos[2] = mMap.GetScreenPositionPixelChar(SVector2(1.0f, 0.0f));
	mStorePos[3] = mMap.GetScreenPositionPixelChar(SVector2(1.0f, 1.0f));
	for(int i = 0; i < 4; ++i)
	{
		mChar[i].SetCharacterPos(mStorePos[i]);
		mChar[i].Load();
	}
}
void PlayState::LoadInitialPositionEnemy()
{
	mEnemyPos[0] = mMap.GetScreenPositionPixelChar(SVector2(7.0f, 7.0f));
	mEnemyPos[1] = mMap.GetScreenPositionPixelChar(SVector2(7.0f, 6.0f));
	for(int i = 0; i < 2; ++i)
	{
		mEnemy[i].SetEnemyPos(mEnemyPos[i]);
		mEnemy[i].Load();
	}
	
}
void PlayState::RenderChar()
{
	for(int i = 0; i < 4; ++i)
	{
		mChar[i].Render(mMap.GetOffset());
	}
}
void PlayState::RenderEnemy()
{
	for(int i(0); i < 2; ++i)
	{
		SVector2 temp(mMap.GetOffset());
		temp.x -= 18.0f;
		mEnemy[i].Render(temp);
	}
}
void PlayState::AttackCharacter()
{
	//SVector2 tempPos(mMap.GetScreenCoordinateChar());//target
	//tempPos = mMap.GetScreenPositionPixelChar(tempPos);
	//for(int i = 0; i < 2; ++i)
	//{
	//	if(tempPos == mChar[i].GetCharacterPos())
	//	{
	//		mChar[i].Defend(&mChar[i]);
	//		mChar[mOrigin].IsClicked();
	//		mOrigin = -1;
	//		break;
	//	}
	//}
//get coor
//loop through coor to find the guy
//attack
	SVector2 tempPos(mMap.GetScreenCoordinateChar());//target
	tempPos = mMap.GetScreenPositionPixelChar(tempPos);
	for(int i = 0; i < 2; ++i)
	{
		if(tempPos == mEnemy[i].GetEnemyPos())//check which one is selected
		{
			for(int j = 0; j < 4; ++j)//check attackers status
			{
				if(mChar[j].GetReadyStatus() && i != j)
				{
					if(mEnemy[i].GetHealth() >= 0)
					{
						mEnemy[i].Defend(mChar[j]);
						mChar[j].IsClicked();
						break;
					}
				}
			}
		}
	}
}

void PlayState::AttackEnemy()
{
	//SVector2 tempPos(mMap.GetScreenCoordinateChar());//target
	//tempPos = mMap.GetScreenPositionPixelChar(tempPos);
	//for(int i = 0; i < 2; ++i)
	//{
	//	if(tempPos == mChar[i].GetCharacterPos())
	//	{
	//		mChar[i].Defend(&mChar[i]);
	//		mChar[mOrigin].IsClicked();
	//		mOrigin = -1;
	//		break;
	//	}
	//}
//get coor
//loop through coor to find the guy
//attack
	SVector2 tempPos(mMap.GetScreenCoordinateChar());//target
	tempPos = mMap.GetScreenPositionPixelChar(tempPos);
	for(int i = 0; i < 4; ++i)
	{
		if(tempPos == mChar[i].GetCharacterPos())//check which one is selected
		{
			for(int j = 0; j < 2; ++j)//check attackers status
			{
				if(mEnemy[j].GetReadyStatus() && i != j)
				{
					if(mChar[i].GetHealth() >= 0)
					{
						mChar[i].Defend(mEnemy[j]);
						mEnemy[j].IsClicked();
						break;
					}
				}
			}
		}
	}
}


void PlayState::MoveCharacter()
{
	//if(mOrigin != -99)
	//{
	//	//destination
	//	SVector2 temp(mMap.GetScreenCoordinateChar());
	//	int destination(mMap.GetIndex2D(temp.x , temp.y));
	//			
	//	//origin
	//	SVector2 temp2(mMap.GetScreenCoordinate(mStorePos[mOrigin]));
	//	int origin(mMap.GetIndex2D(temp2.x, temp2.y));
	//		
	//	for(int j = -1; j < 2; ++j)
	//	{
	//		int tempVal = origin+j;
	//		if(tempVal == destination
	//		||(tempVal+mMap.GetRow() == destination)
	//		||(tempVal-mMap.GetRow() == destination)	
	//		)
	//		{
	//			mChar[mOrigin].SetMoving(true);
	//			//mChar[i].SetMoving(true);
	//			mStorePos[mOrigin] = mMap.GetScreenPositionPixelChar(temp);
	//			mChar[mOrigin].StartMoving(mStorePos[mOrigin]);
	//			//mChar[i].SetCharacterPos(mStorePos[i]);
	//		}
	//	}
	//	mChar[mOrigin].IsClicked();
	//}

	for(int i = 0; i < 4; ++i)
	{
		if(mChar[i].GetReadyStatus())//get ready
		{
			//destination
			SVector2 temp(mMap.GetScreenCoordinateChar());
			int destination(mMap.GetIndex2D(temp.x , temp.y));
				
			//origin
			SVector2 temp2(mMap.GetScreenCoordinate(mStorePos[i]));
			int origin((int)mMap.GetIndex2D(temp2.x, temp2.y));
			
			for(int j = -1; j < 2; ++j)
			{
				int tempVal = origin+j;
				if(tempVal == destination
				||(tempVal+mMap.GetRow() == destination)
				||(tempVal-mMap.GetRow() == destination)	
				)
				{
					mChar[i].SetMoving(true);
					mStorePos[i] = mMap.GetScreenPositionPixelChar(temp);
					mChar[i].StartMoving(mStorePos[i]);
				}
			}
			mChar[i].IsClicked();
		}
	}
}

void PlayState::MoveEnemy()
{
	for(int i = 0; i < 2; ++i)
	{
		if(mEnemy[i].GetReadyStatus())//get ready
		{
			SVector2 temp(mMap.GetScreenCoordinateChar());
			int destination(mMap.GetIndex2D(temp.x , temp.y));
			
			SVector2 temp2(mMap.GetScreenCoordinate(mEnemyPos[i]));
			int origin(mMap.GetIndex2D(temp2.x, temp2.y));
			
			for(int j = -1; j < 2; ++j)
			{
				int tempVal = origin+j;
				if(tempVal == destination
				||(tempVal+mMap.GetRow() == destination)
				||(tempVal-mMap.GetRow() == destination)	
				)
				{
					mEnemy[i].SetMoving(true);
					mEnemyPos[i] = mMap.GetScreenPositionPixelChar(temp);
					mEnemy[i].StartMoving(mEnemyPos[i]);
				}
			}
			mEnemy[i].IsClicked();
		}
	}
}

void PlayState::DrawDebug()
{
	
	for(int i = 0; i < 4; ++i)
	{
		
		if(mChar[i].GetReadyStatus() == true)
		{
			Graphics_DebugCircle(mStorePos[i]+mMap.GetOffset(), 30.0f, 0xffff00);
		}
	}
	for(int i = 0; i < 2; ++i)
	{
		if(mEnemy[i].GetReadyStatus() == true)
		{
			Graphics_DebugCircle(mEnemyPos[i]+mMap.GetOffset(), 30.0f, 0xffff00);
		}
	}
}

void PlayState::DrawText()
{
	char healthBuffer[7][128];
	for(int i = 0; i < 4; ++i)
	{
		if(mChar[i].GetHealth() > 0)
		{
			sprintf_s(healthBuffer[i], 128, "Health : %d", mChar[i].GetHealth());
		}
	}
	for(int i = 0; i < 2; ++i)
	{
		if(mEnemy[i].GetHealth() > 0)
		{
			sprintf_s(healthBuffer[i+4], 128, "Health : %d", mEnemy[i].GetHealth());
		}
	}

	mFont.SetColor(0, 255, 0);
	for(int i = 0; i < 5; ++i)
	{
		if(mChar[i].GetHealth())
		{
			mFont.Print(healthBuffer[i], mChar[i].GetCharacterPos().x + mMap.GetOffset().x , mChar[i].GetCharacterPos().y + mMap.GetOffset().y);
		}
	}
	for(int i = 0; i < 2; ++i)
	{
		if(mEnemy[i].GetHealth() > 0)
		{
			mFont.Print(healthBuffer[i+4], mEnemy[i].GetEnemyPos().x + mMap.GetOffset().x , mEnemy[i].GetEnemyPos().y + mMap.GetOffset().y);
		}
	}
}

bool PlayState::CheckGameOver()
{
	int stateChar(0);
	int stateEnemy(0);

	for(int i = 0; i < 4; ++i)
	{
		if(mChar[i].GetHealth() <= 0)
		{
			stateChar++;
		}
	}

	if(stateChar >= 4)
	{
		return true;
	}

	for(int i = 0; i < 2; ++i)
	{
		if(mEnemy[i].GetHealth() <= 0)
		{
			stateEnemy++;
		}
	}

	if(stateEnemy >= 2)
	{
		return true;	
	}

	return false;
}